{% layout="base.html" %}
{-main_header-}
{-main_header-}

{-main-}
{% if not ioe_cloud_sn then %}
<h1 class="header"> {{_("FreeIOE")}} - {{ioe_sn}} </h1>
{% else %}
<h1 class="header"> {{_("FreeIOE")}} - {{ioe_sn}} - {{ioe_cloud_sn}} </h1>
{% end %}

<h3 class="ui top attached header">
	{{_("Information")}}
</h3>
<div class="ui attached basic segment">
	<div class="ui teal statistic">
		<div class="version value">
			{{version.ioe.ver}}
		</div>
		<div class="label">
			{{_("Version")}}
		</div>
	</div>
	<div class="ui teal statistic">
		<div class="skynet_version value">
			{{version.skynet.ver}}
		</div>
		<div class="label">
			{{_("Skynet Version")}}
		</div>
	</div>
	<div class="ui black statistic">
		<div class="value">
			{{os_id}}
		</div>
		<div class="label">
			{{_("OS")}}
		</div>
	</div>
	<div class="ui blue statistic">
		<div class="value">
			{{mem_info.total}}
		</div>
		<div class="label">
			{{_("Total Memory")}}
		</div>
	</div>
	{% if rollback_time then %}
	<div class="ui orange statistic">
		<div class="rollback_time value">
			{{rollback_time}}
		</div>
		<div class="label">
			{{_("Rollback Time")}}
		</div>
	</div>
	{% end %}
	{% if cloud_status then %}
	<div class="ui green statistic">
		<div class="cloud_uptime value">
		</div>
		<div class="label">
			{{_("Online")}}
		</div>
	</div>
	{% else %}
	<div class="ui red statistic">
		<div class="cloud_uptime value">
		</div>
		<div class="label">
			{{_("Offline")}}
		</div>
	</div>
	{% end %}
</div>

<h3 class="ui top attached header">
	{{_("System Control")}}
</h3>
<div class="ui attached basic segment">
	<div id="mydimmer" class="ui disabled dimmer">
		<div class="ui massive text loader" id="myloader">{{_("Loading")}}</div>
	</div>

	<form class="ui control form">
		{% if rollback_time then %}
		<div class="ui teal icon button upgrade_ack">{{_("Upgrade ACK")}}</div>
		{% else %}
		{% if using_beta then %}
		<div class="ui toggle checked checkbox check_beta">
			<input class="hidden" type="checkbox" checked="true">
			<label>{{_("Check Beta")}}</label>
		</div>
		{% end %}
		<div class="ui teal button check_update">{{_("Check Updates")}}</div>
		{% end %}
		<div class="ui purple button quit">{{_("Restart")}}</div>
		<div class="ui red button reboot">{{_("Device Reboot")}}</div>

		<div class="ui success message"></div>
		<div class="ui error message"></div>
	</form>
</div>

{% if force_upgrade then %}
<div class="ui upgrade attached basic segment">
{% else %}
<div class="ui upgrade attached basic segment" hidden="true">
{% end %}
	<h4 class="ui header">
		{{_("System Upgrade")}}
	</h4>
	<form class="ui sys_upgrade form" action="/sys/upgrade" method="post">
		<input type="hidden" name="from_web" value="1">
		<input type="hidden" name="version" value="latest">
		<input type="hidden" name="skynet_version" value="latest">
		<div class="ui toggle checkbox">
			<input class="hidden" type="checkbox" name="skip_ack">
			<label>{{_("Skip Ack")}}?</label>
		</div>
		<div class="ui skynet toggle checked checkbox">
			<input class="hidden" type="checkbox" checked="true" name="with_skynet">
			<label>{{_("With Skynet")}} ?</label>
		</div>
		<div class="ui primary labeled submit button">
			<div class="ui yellow button">
				<i class="info circle icon"></i> {{_("Upgrade To")}}
			</div>
			<a class="ui basic yellow left pointing label">
				Latest
			</a>
		</div>

		<div class="ui success message"></div>
		<div class="ui error message"></div>
	</form>
</div>

<div class="ui basic segment">
	<div class="ui computer only grid">
		<table class="ui striped basic table">
			<thead>
				<tr>
					<th colspan="2"><h3 class="ui header">{{_("More Information")}}</h3></th>
				</tr>
				<tr>
					<th>{{_("Name")}}</th>
					<th>{{_("Value")}}</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>{{_("SN")}} </td>
					<td>{{ioe_sn}}</td>
				</tr>
				{% if ioe_cloud_sn then %}
				<tr>
					<td>{{_("Cloud ID")}} </td>
					<td>{{ioe_cloud_sn}}</td>
				</tr>
				{% end %}
				<tr>
					<td>{{_("Git Version")}} </td>
					<td>{{version.ioe.git_ver}}</td>
				</tr>
				<tr>
					<td>{{_("Skynet Git Version")}} </td>
					<td>{{version.skynet.git_ver}}</td>
				</tr>
				<tr>
					<td>{{_("CPU Arch")}} </td>
					<td>{{cpu_arch}}</td>
				</tr>
				<tr>
					<td>{{_("CPU Model")}} </td>
					<td>{{cpu_model}}</td>
				</tr>
				<tr>
					<td>{{_("UNAME")}} </td>
					<td>{{uname}}</td>
				</tr>
				<tr>
					<td>{{_("FreeIOE Uptime")}} </td>
					<td>{{skynet_uptime}}</td>
				</tr>
				<tr>
					<td>{{_("OS Uptime")}} </td>
					<td>{{uptime_str}}</td>
				</tr>
				<tr>
					<td>{{_("OS Uptime")}} </td>
					<td class="uptime value"></td>
				</tr>
				<tr>
					<td>{{_("System Time")}} </td>
					<td class="sys_time value">{{sys_time_str}}</td>
				</tr>
			</tbody>
		</table>
	</div>
</div>

{-main-}

{-script-}
<script>
function reset_message(form) {
	form.removeClass('success').removeClass('error');
};
function print_err_message(form, msg) {
	reset_message(form);
	console.log('Exception:' + msg);
	form.find('.ui.error.message').html(msg);
	form.addClass('error');
};
function print_ok_message(form, msg) {
	reset_message(form);
	console.log('OK:' + msg);
	form.find('.ui.success.message').html(msg);
	form.addClass('success');
};
function refresh_page() {
	//window.location.reload();
	window.location.replace("/dashboard");
};
function loader_test() {
	//var loader = $('#myloader');
	var loader = $('#mydimmer');
	loader.removeClass('disabled');
	loader.addClass('active');
};
function sys_control(action, timeout) {
	var cf = $('.ui.control.form');
	$.post("/sys/control", {from_web:true, action:action}, function(data) {
		print_ok_message(cf, '<i>' + data + '</i>');
	})
	.done(function() {
		setTimeout(refresh_page, timeout);
	})
	.fail(function() {
		print_ok_message(cf, '<i>{{_("Failed to send request ")}}' + action + ' </i>');
	});

};
function check_ioe_version() {
	var ioe_label = $('.version.value');
	var skynet_label = $('.skynet_version.value');
	$.get("/sys/check_version", {from_web:true, version:{{version.ioe.ver}}, skynet_version:{{version.skynet.ver}} }, function(data) {
		if (data && data.ioe) {
			if (data.ioe == "release") {
				ioe_label.html('{{version.ioe.ver}} <i class="mini green check icon"></i>');
			} else if (data.ioe == "beta") {
				ioe_label.html('{{version.ioe.ver}} <i class="mini yellow warning sign icon"></i>');
			} else {
				ioe_label.html('{{version.ioe.ver}} <i class="mini red close icon"></i>');
			}
		};
		if (data && data.skynet) {
			if (data.skynet == "release") {
				skynet_label.html('{{version.skynet.ver}} <i class="mini green check icon"></i>');
			} else if (data.skynet == "beta") {
				skynet_label.html('{{version.skynet.ver}} <i class="mini yellow warning sign icon"></i>');
			} else {
				skynet_label.html('{{version.skynet.ver}} <i class="mini red close icon"></i>');
			}
		};
	})
	.done(function() {
	})
	.fail(function() {
		var cf = $('.ui.control.form');
		print_err_message(cf, '<i>{{_("Failed to check system version")}} </i>');
	});
};
function enable_update_btn() {
	$('.ui.button.check_update').removeClass('disabled');
};
function check_for_update(btn) {
	btn.addClass('disabled');
{% if not force_upgrade then %}
	$('.ui.upgrade.segment').hide();
{% end %}

	var cf = $('.ui.control.form');
	reset_message(cf);

	check_ioe_version();

{% if using_beta then %}
	var beta = cf.find('.check_beta').checkbox('is checked');
{% else %}
	var beta = false;
{% end %}
	var version = {{version.ioe.ver}};
	var skynet_version = {{version.skynet.ver}};
	$.get("/sys/check_update", {from_web:true, beta:beta}, function(data) {
		if (data && data.ioe) {
			if (data.ioe.version > version) {
				$('.ui.upgrade.segment').show();
			} else {
{% if not force_upgrade then %}
				print_ok_message(cf, '<i>{{_("There is no new system updates")}} </i>');
{% else %}
				print_ok_message($('.ui.sys_upgrade.form'), '<i>{{_("There is no new system updates")}} </i>');
{% end %}
			}
			upgrade_version = data.ioe.version;
			if (data.ioe.beta) {
				upgrade_version = "beta." + upgrade_version
			}
			$('.ui.sys_upgrade.form .primary.button .label').html(upgrade_version);
			$('.ui.sys_upgrade.form input[name="version"]').val(upgrade_version);
		}
		if (data && data.skynet) {
			if (data.skynet.version > skynet_version) {
				$('.ui.sys_upgrade.form .skynet.checkbox').checkbox('check');
				$('.ui.sys_upgrade.form .skynet.checkbox').removeClass('disabled');
			} else {
				$('.ui.sys_upgrade.form .skynet.checkbox').checkbox('uncheck');
{% if not force_upgrade then %}
				$('.ui.sys_upgrade.form .skynet.checkbox').addClass('disabled');
{% end %}
			}
			upgrade_skynet_version = data.skynet.version;
			if (data.skynet.beta) {
				upgrade_skynet_version = "beta." + upgrade_skynet_version
			}
			$('.ui.sys_upgrade.form .skynet.checkbox label').html("{{_('With Skynet')}} " + upgrade_skynet_version);
			$('.ui.sys_upgrade.form input[name="skynet_version"]').val(upgrade_skynet_version);
		}
	})
	.done(function() {
		setTimeout(enable_update_btn, 3000);
	})
	.fail(function() {
		print_err_message(cf, '<i>{{_("Failed to check system updates")}} </i>');
		setTimeout(enable_update_btn, 5000);
	});
};
$(document).ready(function(){
	{% if is_upgrading then %}
	loader_test();
	{% end %}

	var sf = $('.ui.sys_upgrade.form');
	sf.ajaxForm({
		beforeSend: function () {
			sf.removeClass('success').removeClass('error');
		},
		success: function (response) {
			sf.find('.ui.success.message').html(response);
			sf.addClass('success');
			setTimeout(refresh_page, 10000);
		},
		error: function(xhr) {
			console.log('Exception:' + xhr.responseText);
			sf.find('.ui.error.message').html(xhr.responseText);
			sf.addClass('error');
		}
	});
	$('.ui.button.check_update').click(function() {
		check_for_update($(this));
	});
	$('.ui.control.form .button.upgrade_ack').click(function() {
		sys_control('upgrade_ack', 3000);
	});
	$('.ui.control.form .button.quit').click(function() {
		sys_control('quit', 6000);
	});
	$('.ui.control.form .button.reboot').click(function() {
		sys_control('reboot', 20000);
	});

	// Show count down time
	var set_short_format = function() {
		countdown.setLabels(
			'|s|m|h|d|w|m|y|de|ce|mi',
			'|s|m|h|d|w|m|y|de|ce|mi',
			'',
			'',
			'',
			function(n){ return n.toString(); });
	}

	var now_time = new Date();

	{% if rollback_time then %}
	var rollback_time = {{rollback_time}} * 1000;
	window.setTimeout(function() {
		refresh_page();
	}, rollback_time);
	var rb_timerId =
		countdown(
			now_time.getTime() + rollback_time,
			function(ts) {
				set_short_format();
				$('.rollback_time.value').html(ts.toHTML());
			},
			countdown.DAYS|countdown.HOURS|countdown.MINUTES|countdown.SECONDS);
	{% end %}

	var cloud_last = {{cloud_last}} * 1000;
	var cl_timerId =
		countdown(
			cloud_last,
			function(ts) {
				set_short_format();
				$('.cloud_uptime.value').html(ts.toHTML());
			},
			countdown.DAYS|countdown.HOURS|countdown.MINUTES|countdown.SECONDS);

	var uptime = {{uptime}} * 1000;
	var up_timerId =
		countdown(
			now_time.getTime() - uptime,
			function(ts) {
				countdown.resetLabels();
				$('.uptime.value').html(ts.toHTML("strong"));
			},
			countdown.DAYS|countdown.HOURS|countdown.MINUTES|countdown.SECONDS);

});
</script>
{-script-}
